package main;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import javax.swing.JPanel;

/**
 * MapPanel draws map
 * @author 12817152
 *
 */
public class MapPanel extends JPanel{
	Land[][] map;
	boolean blacked = false, newGame = true;
	TurnManager t;
	boolean[][] data;
	Player currentP;

	/**
	 * constructor
	 * @param m - map
	 */
	public MapPanel(Land[][] m) {
		map = m;
		repaint();
	}

	/**
	 * draws a black screen on the map
	 */
	public void blackout() {
		blacked = true;
		repaint();
		newGame = false;
	}

	/**
	 * returns panel to the map (instead of black)
	 */
	public void unblack() {
		blacked = false;
		repaint();
	}

	/**
	 * fills panel with black
	 * @param g - graphics
	 */
	public void fillBlack(Graphics g) {
		g.fillRect(0, 0, getWidth(), getHeight());
	}

	/**
	 * updates current player
	 * @param p - player
	 */
	public void updatePlayer(Player p) {
		currentP = p;
	}

	/**
	 * updates map painting based on movements of units
	 * @param g - graphics
	 */
	public void updateMap(Graphics g) {
		Graphics2D g2d = (Graphics2D)g;
		Font font = new Font("Arial Bold", Font.PLAIN, (int)((getWidth()/map.length+getHeight()/map[1].length)/3.5));	    
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

		for(int i = 0; i < map.length; i++){
			for(int j = 0; j < map[1].length; j++) {

				g.setColor(map[i][j].getColor());
				g.fillRect((int)((i)*(getWidth()/(double)map.length)), (int)((j)*(getHeight()/(double)map[1].length)), (int)(getWidth()/(double)map.length+2), (int)(getHeight()/(double)map[1].length+2));
				map[i][j].cleanUnit();
				
				try {
					data = currentP.getFogData();

					if(!(data[i][j])) {
						g.setColor(map[i][j].getColor().darker());
						g.fillRect((int)((i)*(getWidth()/(double)map.length)), (int)((j)*(getHeight()/(double)map[1].length)), (int)(getWidth()/(double)map.length+2), (int)(getHeight()/(double)map[1].length+2));
					}
				} catch(NullPointerException e1) {
					g.setColor(map[i][j].getColor().darker());
					g.fillRect((int)((i)*(getWidth()/(double)map.length)), (int)((j)*(getHeight()/(double)map[1].length)), (int)(getWidth()/(double)map.length+2), (int)(getHeight()/(double)map[1].length+2));
				}

				if(map[i][j].getColony() != null) {
					if(map[i][j].getColony().getOwner() == currentP || data[i][j]) {
						g.setColor(map[i][j].getColony().getColor());
						g.fillRect((int)((i)*(getWidth()/(double)map.length)), (int)((j)*(getHeight()/(double)map[1].length)), (int)(getWidth()/(double)map.length+2), (int)(getHeight()/(double)map[1].length+2));
					}
				}

				if(map[i][j].getCity() != null) {
					g.setColor(map[i][j].getCity().getColor());
					g.fillRect((int)((i)*(getWidth()/(double)map.length)), (int)((j)*(getHeight()/(double)map[1].length)), (int)(getWidth()/(double)map.length+2), (int)(getHeight()/(double)map[1].length+2));
				}

				try {
					if(map[i][j].getUnit() != null) {
						if(map[i][j].getUnit().owner == currentP || data[i][j]) {
							g.setColor(map[i][j].getUnit().getColor());
							g.fillOval((int)((i)*(getWidth()/(double)map.length)), (int)((j)*(getHeight()/(double)map[1].length)), (int)(getWidth()/(double)map.length), (int)(getHeight()/(double)map[1].length));
							g.setColor(Color.WHITE);
							g2d.setFont(font);
							if(map[i][j].getUnit().getName().equals("Can"))
								g.drawString("CAN", (int)((i+0.15)*(getWidth()/(double)map.length)),(int)((j+0.75)*(getHeight()/(double)map[1].length)));
							else if(map[i][j].getUnit().getName().equals("Foot"))
								g.drawString("FS", (int)((i+0.15)*(getWidth()/(double)map.length)),(int)((j+0.75)*(getHeight()/(double)map[1].length)));
							else if(map[i][j].getUnit().getName().equals("Cav"))
								g.drawString("CAV", (int)((i+0.15)*(getWidth()/(double)map.length)),(int)((j+0.75)*(getHeight()/(double)map[1].length)));
							else if(map[i][j].getUnit().getName().equals("Scout"))
								g.drawString("S", (int)((i+0.15)*(getWidth()/(double)map.length)),(int)((j+0.75)*(getHeight()/(double)map[1].length)));
							else if(map[i][j].getUnit().getName().equals("Tanks"))
								g.drawString("T", (int)((i+0.15)*(getWidth()/(double)map.length)),(int)((j+0.75)*(getHeight()/(double)map[1].length)));

						}
					}
				} catch(NullPointerException e) {}
			}
		}
	}

	/**
	 * paints panel
	 */
	public void paint(Graphics g) {
		if(blacked) {
			fillBlack(g);
			g.setColor(Color.WHITE);
			g.drawString("your move, mate", getWidth()/2-45, getHeight()/2);
		} else if(newGame) {
			g.setColor(Color.WHITE);
			fillBlack(g);
			g.setColor(Color.BLACK);
			g.drawString("SOCIETY IN A BOX", getWidth()/2-45, getHeight()/2-50);
			g.drawString("like a government in a box, but more ambitious", getWidth()/2-120, getHeight()/2-25);
			g.drawString("this is a fancy title screen. it needs instructions.", getWidth()/2-120, getHeight()/2+45);
		}else {
			updateMap(g);
		}
	}
}
